package cn.xiaopengstack.domain.activity.service.trialcal.node;

import cn.xiaopengstack.domain.activity.model.entity.MarketProductEntity;
import cn.xiaopengstack.domain.activity.model.entity.TrialCalculateEntity;
import cn.xiaopengstack.domain.activity.model.valobj.CrowTagDetailVO;
import cn.xiaopengstack.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.xiaopengstack.domain.activity.service.trialcal.AbstractGroupBuyMarketSupport;
import cn.xiaopengstack.domain.activity.service.trialcal.factory.DefaultActivityStrategyFactory;
import cn.xiaopengstack.types.framework.strategy.tree.StrategyHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author jiangyangang
 */
@Component
public class TagNode extends AbstractGroupBuyMarketSupport {

    @Resource
    private EndNode endNode;

    @Override
    protected TrialCalculateEntity doApply(MarketProductEntity param, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {

        // 获取人群
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();

        CrowTagDetailVO crowTagDetailVO = activityRepository.queryUserTagDetailVO(param.getUserId(), groupBuyActivityDiscountVO.getTagId());
        // 当前用户未被打标
        if (Objects.isNull(crowTagDetailVO)) {
            dynamicContext.setVisible(true);
            dynamicContext.setEnable(true);
            return route(param, dynamicContext);
        }

        // 用户被打标
        dynamicContext.setVisible(groupBuyActivityDiscountVO.isVisible());
        dynamicContext.setEnable(groupBuyActivityDiscountVO.isEnable());
        return route(param, dynamicContext);

    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialCalculateEntity> get(MarketProductEntity param, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return endNode;
    }
}
